package com.yuju.hotel.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.hotel.dao.IRoomTypeDao;
import com.yuju.hotel.dao.entity.RoomType;
import com.yuju.hotel.model.PriceDetailModel;
import com.yuju.hotel.model.RoomModel;
import com.yuju.hotel.model.RoomTypeModel;
import com.yuju.hotel.service.IPriceDetailService;
import com.yuju.hotel.service.IRoomService;
import com.yuju.hotel.service.IRoomTypeService;
import com.yuju.util.Utils;

/**
 * 
 * 类名：RoomTypeServiceImpl  <br />
 *
 * 功能：
 *
 * @author : zhousc <br />
 * @Date : 2017年11月27日 下午2:21:30  <br />
 * @version : 1.1 <br />
 */
@Service
public class RoomTypeServiceImpl extends AbstractBaseService<RoomTypeModel, RoomType, String> implements IRoomTypeService{

	@Autowired
	private IRoomTypeDao roomTypeDao;
	@Autowired
	private IRoomService roomService;
	@Autowired
	private IPriceDetailService priceDetailService;
	
	@Override
	public IBaseDao<RoomTypeModel, RoomType, String> getDao() {
		// TODO Auto-generated method stub
		return roomTypeDao;
	}
	/**
	 * 
	 * 功能: <br/>新增房型
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月27日 下午2:22:54<br/>
	 * 
	 * @param roomTypeModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#save(java.lang.Object)
	 */
	@Override
	public int save(RoomTypeModel roomTypeModel){
		//添加房型
		roomTypeModel.setId(Utils.getUUID());
		roomTypeModel.setGmtCreate(new Date());
		roomTypeModel.setGmtModified(new Date());
		this.getDao().saveSelective(roomTypeModel);
		
		//价格方案增加该房型
		PriceDetailModel priceDetailModel = new PriceDetailModel();
		priceDetailModel.setHotelId(roomTypeModel.getHotelId());
		priceDetailModel.setGroupId(roomTypeModel.getGroupId());
		priceDetailModel.setRoomTypeId(roomTypeModel.getId());		
		priceDetailModel.setPrice(roomTypeModel.getPrice());
		priceDetailService.saveRoomTypeAfter(priceDetailModel);
		
		return 1;
	}
	/**
	 * 
	 * 功能: <br/>更新房型信息
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月27日 下午2:23:29<br/>
	 * 
	 * @param roomTypeModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#updateSelective(java.lang.Object)
	 */
	@Override
	public int updateSelective(RoomTypeModel roomTypeModel){
		roomTypeModel.setGmtModified(new Date());
		//更新房型数据
		this.getDao().updateSelective(roomTypeModel);
		//更新价格方案明细中的门市价
		PriceDetailModel priceDetailModel = new PriceDetailModel();
		priceDetailModel.setHotelId(roomTypeModel.getHotelId());
		priceDetailModel.setGroupId(roomTypeModel.getGroupId());
		priceDetailModel.setRoomTypeId(roomTypeModel.getId());		
		priceDetailModel.setPrice(roomTypeModel.getPrice());
		priceDetailService.updRoomTypeAfter(priceDetailModel);
		
		return 1;
	}
	/**
	 * 
	 * 功能:删除房型 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月30日 上午9:37:58<br/>
	 * 
	 * @param id
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#del(java.lang.Object)
	 */
	@Override
	public int del(String id){
		//首先判断该房型下是否存在房间信息，如有房间信息，不能删除房型
		RoomModel roomModel = new RoomModel();
		roomModel.setRoomTypeId(id);
		List<RoomModel> roomList = roomService.list(roomModel);
		if(roomList.size() != 0){
			return 0; 
		}
		//如果该房型下没有房间信息，执行删除，并删除相关价格明细表中该房型的价格明细
		this.getDao().del(id);
		priceDetailService.delPriceDetailByRoomTypeId(id);
		return 1;
	}
	/**
	 * 
	 * 功能:重名验证 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月14日 上午10:31:52<br/>
	 * 
	 * @param roomTypeModel
	 * @return <br/>
	 * @see com.yuju.hotel.service.IRoomTypeService#selectByName(com.yuju.hotel.model.RoomTypeModel)
	 */
	@Override
	public boolean selectByName(RoomTypeModel roomTypeModel) {
		// TODO Auto-generated method stub
		//定义返回值
		boolean result = false;
		//装载数据查询数据库
		RoomTypeModel model = new RoomTypeModel();
		model.setName(roomTypeModel.getName());
		model.setHotelId(roomTypeModel.getHotelId());
		
		//修改时验证重名
		if (Utils.isNotNull(roomTypeModel.getId())) {
			RoomTypeModel roomTypeModel1 = convert(this.getDao().getObj(roomTypeModel.getId()));
			if (roomTypeModel.getName().equals(roomTypeModel1.getName())) {
			        result = true;
			    } else {
			   	 	Map<Object, Object> beanMap = getBeanMap(model);
			        List<RoomTypeModel> list = this.getDao().list(beanMap);
			        if (list.size() == 0) {
			            result = true;
			        }
			    }
		//新增验证重名
		}else {
			Map<Object, Object> beanMap = getBeanMap(model);
			    List<RoomTypeModel> list = this.getDao().list(beanMap);
			    if (list.size() == 0) {
			        result = true;
			    }
			}
		return result;
	}
}
